package com.woniuxy.enjoylife.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.Module;
import com.woniuxy.enjoylife.entity.Activity;
import com.woniuxy.enjoylife.entity.Activity_comment;
import com.woniuxy.enjoylife.entity.User_activity;
import com.woniuxy.enjoylife.entity.dto.*;
import com.woniuxy.enjoylife.enums.StateEnum;
import com.woniuxy.enjoylife.mapper.ActivityMapper;
import com.woniuxy.enjoylife.mapper.UserMapper;
import com.woniuxy.enjoylife.service.ActivityService;
import com.woniuxy.enjoylife.utils.PageResult;
import com.woniuxy.enjoylife.utils.PageUtil;
import com.woniuxy.enjoylife.utils.ResponseResult;
import com.woniuxy.enjoylife.utils.SetUid;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/activity")
public class ActivityController {
    @Resource
    private ActivityMapper activityMapper;
    @Resource
    private ActivityService activityService;
    @Resource
    private ActivityService activityServiceImpl;
    @Resource
    private HttpServletRequest request;

    //通过活动id去查当前活动信息以及发起者，参与者
    @GetMapping("/findActivityUsersDtoById")
    public ResponseResult findUsersByAid(int id){
        ActivityUsersDto activityUsersDto = activityMapper.findActivityUsersDtoById(id);
        ResponseResult responseResult = new ResponseResult();
        if(activityUsersDto == null){
            responseResult.error();
        }else{
            responseResult.success();
            responseResult.setData(activityUsersDto);
        }
        return responseResult;
    }

    //通过用户id去中间表删除该用户参加的该活动
    @RequestMapping("/delByUid")
    public ResponseResult delByUid(int uid,int aid) {
        ResponseResult responseResult = new ResponseResult();
        //删除前先判断，只有在报名中阶段才可以删除
        int activityResult = activityMapper.findResultById(aid);
        if (activityResult == 1) {
            if (activityService.delByUid(uid, aid)) {
                responseResult.success();
                responseResult.setMessage("删除成功!");
            } else {
                responseResult.error();
                responseResult.setMessage("删除失败!请稍后重试！");
            }
        } else {
            responseResult.error();
            responseResult.setState(StateEnum.ACTIVITYEND);
            responseResult.setMessage("活动已结束！不能修改名单！");
        }
        return responseResult;
    }

    //通过用户id数组遍历批量删除用户参与活动
    @RequestMapping("/delBatchByUid")
    public ResponseResult delBatchByUid(int ids[],int aid){
        ResponseResult responseResult = new ResponseResult();
        //删除前先判断，只有在报名中阶段才可以删除
        int activityResult = activityMapper.findResultById(aid);
        //调用方法批量删除
        if (activityResult == 1) {
            if (activityService.delBatchByUid(ids,aid)) {
                responseResult.success();
                responseResult.setMessage("删除成功!");
            } else {
                responseResult.error();
                responseResult.setMessage("删除失败!请稍后重试！");
            }
        } else {
            responseResult.error();
            responseResult.setState(StateEnum.ACTIVITYEND);
            responseResult.setMessage("活动已结束！不能修改名单！");
        }
        return responseResult;
    }

    /**
     * 根据活动id删除（将status改为1）
     */
    @RequestMapping("/del")
    public ResponseResult del(int id) {
        ResponseResult responseResult = new ResponseResult();
        if (activityService.del(id)) {
            responseResult.success();
            responseResult.setMessage("删除成功！");
        } else {
            responseResult.error();
            responseResult.setMessage("删除失败！");
        }
        return responseResult;
    }

    /**
     * 小区新增活动
     */
    @RequestMapping("/communityAddActivity")
    public ResponseResult communityAddActivity(Activity activity){
        //新添加的默认result 1,status 0
        activity.setResult(1).setStatus(0);
        ResponseResult responseResult = new ResponseResult();
        if (activityService.communityAddActivity(activity)) {
            responseResult.success();
            responseResult.setMessage("发布成功！");
        } else {
            responseResult.error();
            responseResult.setMessage("发布失败！请稍后重试！");
        }
        return responseResult;
    }

    /**
     *  平台活动管理，查询所有小区的活动
     */
    @RequestMapping("/platFindAllActivity")
    public PageResult<List<ActivityCommunityDto>> platFindAllActivity(int current, int size){
        PageResult<List<ActivityCommunityDto>> pageResult = activityService.platFindAllActivity(current, size);
        return pageResult;
    }

    /**
     * 用户查询自己发表的活动(用户id为活动表中发表人id)
     */
    @RequestMapping("/userFindOwnActivity")
    public PageResult<List<ActivityUsersDto>> userFindOwnActivity(int current,int size){
        //根据登录获取
        int uid = (int) request.getSession().getAttribute("id");
        PageResult<List<ActivityUsersDto>> pageResult = activityService.userFindOwnActivity(uid, current, size);
        return pageResult;
    }

    /**
     *  根据活动id去查询该活动的评论及用户
     */
    @RequestMapping("/findActivityCommentByAid")
    public PageResult<List<ActivityCommentUserDto>> findActivityCommentByAid(int aid,int current,int size){
        PageResult<List<ActivityCommentUserDto>> pageResult = activityService.findActivityCommentByAid(aid, current, size);
        return pageResult;
    }

    /**
     *  用户查询自己参与的活动（去用户-活动中间表查询）
     */
    @RequestMapping("/userFindJoinActivity")
    public PageResult<List<ActivityUsersDto>> userFindJoinActivity(int current,int size){
        //根据登录获取
        int uid = (int) request.getSession().getAttribute("id");
        PageResult<List<ActivityUsersDto>> pageResult = activityService.userFindJoinActivity(uid, current, size);
        return pageResult;
    }

    //查询所有活动
    @RequestMapping("/pageActivity")
    public PageResult<ActivityShowDto> selectActivity(int page, int size){
        int userid = (int) request.getSession().getAttribute("id");
        return activityServiceImpl.selectActivity(page,size,userid);
    }

    //查找单个用户的活动(id,模糊查询)
    @RequestMapping("/pageOneActivity")
    public List<ActivityShowDto> selectOneActivity(Activity activity){
        return activityServiceImpl.selectOneActivity(activity);
    }

    //新增
    @RequestMapping("/addActivity")
    public ResponseResult addActivity(Activity activity){
        ResponseResult responseResult = new ResponseResult();
        if (activity.getContent() != null) {
            responseResult.success();
        } else {
            responseResult.error();
        }
        activityServiceImpl.addActivity(activity);
        return responseResult;
    }


    //修改
    @RequestMapping("/updateActivity")
    public ResponseResult updateActivity(Activity activity){
        ResponseResult responseResult = new ResponseResult();
        responseResult.success();
        activityServiceImpl.updateActivity(activity);
        return responseResult;
    }

    //删除
    @RequestMapping("/deleteActivity")
    public ResponseResult deleteActivity(int id){
        ResponseResult responseResult = new ResponseResult();
        responseResult.success();
        activityServiceImpl.deleteActivity(id);
        return responseResult;
    }

//用户界面=======================================================================
//    展示所用活动
@GetMapping("findAllActivity/{currentPage}/{size}")
@ResponseBody
public  ResponseResult findByPages(@PathVariable("currentPage") Integer currentPage,@PathVariable("size") Integer size){
//    int uid = (int)request.getSession().getAttribute("id");
//    QueryWrapper wrapper = SetUid.bindUid(uid);
//    QueryWrapper<Activity> queryWrapper = new QueryWrapper<Activity>().eq("uid", uid);
    ResponseResult responseResult = new ResponseResult();
    Page pageInfo = new Page(currentPage,size);


    Page page = activityService.page(pageInfo);
    PageResult pageResult = PageUtil.setPageResult(page);
    pageResult.setData(page.getRecords());


    responseResult.setData(pageResult);
    if (pageResult.getData()!=null){
        responseResult.success();
    }else {
        responseResult.error();
    }

    return responseResult;
}
//    我发布的活动
    @GetMapping("findByPage/{currentPage}/{size}")
    @ResponseBody
    public  ResponseResult findByPage(@PathVariable("currentPage") Integer currentPage,@PathVariable("size") Integer size){
        int uid = (int)request.getSession().getAttribute("id");
        QueryWrapper wrapper = SetUid.bindUid(uid);
        QueryWrapper<Activity> queryWrapper = new QueryWrapper<Activity>().eq("uid", uid);
        ResponseResult responseResult = new ResponseResult();
        Page pageInfo = new Page(currentPage,size);


        Page page = activityService.page(pageInfo,queryWrapper);
        PageResult pageResult = PageUtil.setPageResult(page);
        pageResult.setData(page.getRecords());


        responseResult.setData(pageResult);
        if (pageResult.getData()!=null){
            responseResult.success();
        }else {
            responseResult.error();
        }

        return responseResult;
    }

/**
 * 查找活动发起用户和活动内容
 */
@GetMapping("activityAndUser/{id}")
@ResponseBody
public ResponseResult activityAndUser(@PathVariable("id") Integer id){
    ResponseResult responseResult = new ResponseResult();

    ActivityUsersDto activityAndUserById = activityService.findActivityAndUserById(id);

    responseResult.setData(activityAndUserById);
    if (responseResult.getData()!=null){
        responseResult.success();
    }else {
        responseResult.error();
    }

    return responseResult;
}

/**
 * 发布新活动
 */

@PostMapping("/addActivitys")
public  ResponseResult addActivitys(Activity activity){

    int uid = (int)request.getSession().getAttribute("id");
    ResponseResult responseResult = new ResponseResult();
    activity.setUid(uid).setStatus(0).setResult(1);
    System.out.println("活动内容"+activity);
    boolean result = activityService.saveOrUpdate(activity);
if (result){
    responseResult.success();
}else {
    responseResult.error();
}

    return responseResult;
}


}
